package com.peas.platform.module.icims.base.approve.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomain;
import com.peas.platform.framework.mybatisplus.po.BasePo;
import com.peas.platform.framework.mybatisplus.service.BaseServiceImpl;
import com.peas.platform.module.icims.base.approve.domain.ApproveOrderTobeDomain;
import com.peas.platform.module.icims.base.approve.dto.ApproveOrderDto;
import com.peas.platform.module.icims.base.approve.dto.ApproveOrderTobeDto;
import com.peas.platform.module.icims.base.approve.enums.ApproveOrderBusinessType;
import com.peas.platform.module.icims.base.approve.enums.ApproveOrderTobeState;
import com.peas.platform.module.icims.base.approve.enums.ApproveOrderTobeType;
import com.peas.platform.module.icims.base.approve.service.ApproveOrderService;
import com.peas.platform.module.icims.base.approve.service.ApproveOrderTobeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ApproveOrderTobeServiceImpl extends BaseServiceImpl<ApproveOrderTobeDto> implements ApproveOrderTobeService {

    @Resource
    private ApproveOrderTobeDomain approveOrderTobeDomain;
    @Resource
    private ApproveOrderService approveOrderService;

    @Override
    public BaseDomain<ApproveOrderTobeDto, ? extends BasePo> getDomain() {
        return approveOrderTobeDomain;
    }

    @Transactional
    @Override
    public void createApproveOrderTobe(Long approveOrderId, ApproveOrderTobeType type, String title, String content, Long userId, ApproveOrderTobeState state) {
        ApproveOrderTobeDto aotd = new ApproveOrderTobeDto();
        aotd.setApproveOrderId(approveOrderId);
        aotd.setApproveOrderTobeType(type.getKey());//1:待办，2:待阅
        aotd.setApproveOrderTobeTitle(title);
        aotd.setApproveOrderTobeContent(content);
        aotd.setReceiveUserId(userId);
        aotd.setReadTime(null);
        aotd.setProcessTime(null);
        aotd.setState(state.getKey());
        this.save(aotd);
    }

    @Transactional
    @Override
    public void removeApproveOrderTobe(ApproveOrderBusinessType aobt, Long businessId) {
        ApproveOrderDto c = new ApproveOrderDto();
        c.setBusinessType(aobt.getKey());
        c.setBusinessId(businessId);
        List<ApproveOrderDto> aods = approveOrderService.list(c);
        CollUtil.forEach(aods, (CollUtil.Consumer<ApproveOrderDto>) (aod, index) -> {
            ApproveOrderTobeDto c1 = new ApproveOrderTobeDto();
            c1.setApproveOrderId(aod.getId());
            c1.setState(ApproveOrderTobeState.doing.getKey());
            List<ApproveOrderTobeDto> aotds = this.list(c1);
            if (CollUtil.isNotEmpty(aotds)) {
                this.removeBatchByList(aotds);
            }
        });
    }
}
